home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / test_lay / tglayout.bas < prev   
BASIC Source File  |  1994-05-18  |  7KB  |  182 lines

  1. ' TGLayout.Bas - Routines used with True Grid
  2. ' 94/05/06 Copyright 1994, Larry Rebich, The Bridge, Inc.
  3. ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
  4.     Option Explicit
  5.     DefInt A-Z
  6.  
  7. '   GetTempFileName
  8. Declare Function GetTempFileName Lib "Kernel" (ByVal cDriveLetter As Integer, ByVal lpPrefixString As String, ByVal wUnique As Integer, ByVal lpTempFileName As String) As Integer
  9.  
  10. '   Grid Layout Type
  11. Type TrueGridLayout
  12.     WhichLen As Integer 'length of which
  13.     Which As String     'which grid, TablePeople, etc.
  14.     TypeLen As Integer  'length of type
  15.     Type As String      'Factory or Current, could be anything
  16.     LastUpdatedLen As Integer   'length of date and time
  17.     LastUpdated As String       'last updated, date and time
  18.     ValueLen As Integer 'length of the layout
  19.     Value As Variant    'the layout, 2000 or more characters
  20. End Type
  21.  
  22.  
  23. Function GridGetLayout (TheFile As String, Which As String, What As String, TheGrid As Control) As Integer
  24. ' Logic: Read the file sequentially looking for the required saved layout.
  25. '        If found then set the grid with the stored layout information.
  26. '        If not found then exit without setting the grid.
  27.     Dim ALayout As TrueGridLayout
  28.     Dim fi As Integer       'input file
  29.     Dim bi As Integer       'which byte in input file
  30.     bi = 1                  'first byte
  31.     fi = FreeFile           'a file handle
  32.     GridGetLayout = False   'not changed
  33.     Open TheFile For Binary As #fi
  34.     Do Until bi > LOF(fi)
  35.         Get #fi, bi, ALayout.WhichLen
  36.         ALayout.Which = String$(ALayout.WhichLen, Chr$(0))
  37.         Get #fi, , ALayout.Which
  38.         
  39.         Get #fi, , ALayout.TypeLen
  40.         ALayout.Type = String$(ALayout.TypeLen, Chr$(0))
  41.         Get #fi, , ALayout.Type
  42.         
  43.         Get #fi, , ALayout.LastUpdatedLen
  44.         ALayout.LastUpdated = String$(ALayout.LastUpdatedLen, Chr$(0))
  45.         Get #fi, , ALayout.LastUpdated
  46.         
  47.         Get #fi, , ALayout.ValueLen
  48.         ALayout.Value = String$(ALayout.ValueLen, Chr$(0))
  49.         Get #fi, , ALayout.Value
  50.         
  51.         If ALayout.Which = Which And ALayout.Type = What Then   'match
  52.             If TheGrid.Layout <> ALayout.Value Then 'no change
  53.                 On Error Resume Next
  54.                 TheGrid.Layout = ALayout.Value
  55.                 GridGetLayout = True    'changed
  56.             End If
  57.             Exit Do
  58.         End If
  59.         bi = Seek(fi)                   'next input location
  60.     Loop
  61.     Close #fi
  62. End Function
  63.  
  64. Function GridSaveLayout (TheFile As String, Which As String, What As String, TheGrid As Control) As Integer
  65. ' Logic: Read the entire input file and write a completly new one.
  66. '        If the layout is found in the file then replace it.
  67. '        If the layout is not found then add it to the end of the file.
  68. '        A temporary output file is created then copied to the "real" one.
  69.     
  70.     Dim ALayout As TrueGridLayout
  71.     Dim fi As Integer       'input file
  72.     Dim fo As Integer       'output file
  73.     Dim foTemp As String    'temporary name
  74.     Dim bi As Long          'which byte in input file
  75.     Dim bo As Long          'which byte in output file
  76.     Dim LayoutReplacedSw As Integer 'replaced
  77.     Dim dt As String        'date and time
  78.     dt = Format$(Now, "ddddd") & "-" & Format$(Now, "ttttt")
  79.     
  80.     GridSaveLayout = False  'not saved
  81.  
  82.     bi = 1      'beginning of input file
  83.     bo = 1      'beginning of output file
  84.     
  85.     foTemp = TempNamePlease()   'temporary file name
  86.     
  87.     fo = FreeFile
  88.     Open foTemp For Binary As #fo
  89.     
  90.     fi = FreeFile
  91.     Open TheFile For Binary As #fi
  92.     
  93.     While bi < LOF(fi)      'process the file
  94.         Get #fi, bi, ALayout.WhichLen
  95.         ALayout.Which = String$(ALayout.WhichLen, Chr$(0))
  96.         Get #fi, , ALayout.Which
  97.         
  98.         Get #fi, , ALayout.TypeLen
  99.         ALayout.Type = String$(ALayout.TypeLen, Chr$(0))
  100.         Get #fi, , ALayout.Type
  101.         
  102.         Get #fi, , ALayout.LastUpdatedLen
  103.         ALayout.LastUpdated = String$(ALayout.LastUpdatedLen, Chr$(0))
  104.         Get #fi, , ALayout.LastUpdated
  105.         
  106.         Get #fi, , ALayout.ValueLen
  107.         ALayout.Value = String$(ALayout.ValueLen, Chr$(0))
  108.         Get #fi, , ALayout.Value
  109.         
  110.         bi = Seek(fi)       'next location
  111.         If ALayout.Which = Which And ALayout.Type = What Then   'match
  112.             If Not LayoutReplacedSw Then        'no dups
  113.                 If ALayout.Value <> TheGrid.Layout Then     'no change
  114.                     ALayout.WhichLen = Len(Which)
  115.                     ALayout.Which = Which
  116.                     ALayout.TypeLen = Len(What)
  117.                     ALayout.Type = What
  118.                     ALayout.LastUpdatedLen = Len(dt)
  119.                     ALayout.LastUpdated = dt
  120.                     ALayout.ValueLen = Len(TheGrid.Layout)
  121.                     ALayout.Value = TheGrid.Layout
  122.                     LayoutReplacedSw = True
  123.                 Else        'same, so do nothing
  124.                     Close #fi, #fo
  125.                     Kill foTemp
  126.                     Exit Function
  127.                 End If
  128.             End If
  129.         End If
  130.  
  131.         Put #fo, bo, ALayout.WhichLen   'write the record
  132.         Put #fo, , ALayout.Which
  133.         Put #fo, , ALayout.TypeLen
  134.         Put #fo, , ALayout.Type
  135.         Put #fo, , ALayout.LastUpdatedLen
  136.         Put #fo, , ALayout.LastUpdated
  137.         Put #fo, , ALayout.ValueLen
  138.         Put #fo, , ALayout.Value
  139.         bo = Seek(fo)
  140.     Wend
  141.     ' was it replaced?
  142.     If Not LayoutReplacedSw Then        'append to end
  143.         ALayout.WhichLen = Len(Which)
  144.         ALayout.Which = Which
  145.         ALayout.TypeLen = Len(What)
  146.         ALayout.Type = What
  147.         ALayout.LastUpdatedLen = Len(dt)
  148.         ALayout.LastUpdated = dt
  149.         ALayout.ValueLen = Len(TheGrid.Layout)
  150.         ALayout.Value = TheGrid.Layout
  151.         Put #fo, bo, ALayout.WhichLen   'put the new record
  152.         Put #fo, , ALayout.Which
  153.         Put #fo, , ALayout.TypeLen
  154.         Put #fo, , ALayout.Type
  155.         Put #fo, , ALayout.LastUpdatedLen
  156.         Put #fo, , ALayout.LastUpdated
  157.         Put #fo, , ALayout.ValueLen
  158.         Put #fo, , ALayout.Value
  159.     End If
  160.     Close #fi, #fo
  161.     On Error Resume Next        'no error stopping
  162.     FileCopy foTemp, TheFile    'replaces old one as it copies
  163.     Kill foTemp
  164.     GridSaveLayout = True       'saved
  165. End Function
  166.  
  167. Function TempNamePlease () As String
  168. ' Get a temporary file name, concept from MSDN CD7
  169.     Dim t As String     'temporary string
  170.     Dim b As Integer    'buffer size
  171.     Dim r As Integer    'return value
  172. '    b = 144            'suggested buffer size
  173.     b = 160             'and a little extra, make sure have some spaces
  174.     t = String$(b, " ") 'load buffer
  175.     r = GetTempFileName(0, "spa", 0, t) 'API to get the name
  176.     t = Trim$(t)                'dump extra spaces
  177.     t = Left$(t, Len(t) - 1)    'dump chr$(0)
  178.     t = LCase$(t)               'lower case looks better?
  179.     TempNamePlease = t     'return it
  180. End Function
  181.  
  182.